package com.eaoa.uc.account.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eaoa.constants.AttachmentConstants;
import com.eaoa.query.pub.PubCityQuery;
import com.eaoa.query.pub.PubCustomerQuery;
import com.eaoa.uc.customer.form.CustomerForm;
import com.eaoa.utils.AttachmentUtils;
import com.eaoa.vo.pub.PubCustomer;
import com.stony.core.struts.action.BaseAction;
import com.stony.core.util.CoreUtils;

public class AccountRegisterCustomerPreAction extends BaseAction{

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		CustomerForm myform = (CustomerForm) form;
		if(StringUtils.isBlank(myform.getCustomerno())){
			String cusomernostr = request.getAttribute("customerno").toString();
			myform.setCustomerno(cusomernostr);
		}
		Long customerno = CoreUtils.parseLong(myform.getCustomerno());

		if (customerno!=null) {
			if (!isReEntry(request)) {
				PubCustomer po = PubCustomerQuery.loadByNo(customerno);
				myform.setVO(po);
			}
			Map attachments = AttachmentUtils.getAttachmentMap(AttachmentConstants.ATTACHMENTTYPE_CUSTOMER,customerno);
			request.setAttribute("attachments", attachments);
		}
		
		Long provinceno = CoreUtils.parseLong(myform.getAddressprovince());
		request.setAttribute("city", PubCityQuery.getCitys(provinceno, new Long(2)));

		Long cityno = CoreUtils.parseLong(myform.getAddresscity());
		request.setAttribute("district", PubCityQuery.getCitys(cityno, new Long(3)));
		
		return mapping.findForward("success");
	}
}
